import { AddressList } from "vant";
import { createRouter, createWebHashHistory } from "vue-router";
import { getCache } from "/@utils/tools.js";

const Login = () => import(/* webpackChunkName: "apfc" */ "../views/Login.vue");
const Main = () => import(/* webpackChunkName: "apfc" */ "../views/Main.vue");
const Home = () => import(/* webpackChunkName: "apfc" */ "../views/Home.vue");
const Categories = () =>
  import(/* webpackChunkName: "apfc" */ "../views/Categories.vue");
const Cart = () => import(/* webpackChunkName: "apfc" */ "../views/Cart.vue");
const Search = () =>
  import(/* webpackChunkName: "apfc" */ "../views/Search.vue");
const Detail = () =>
  import(/* webpackChunkName: "apfc" */ "../views/Detail.vue");
const User = () => import(/* webpackChunkName: "apfc" */ "../views/User.vue");
const User_AddressList = () =>
  import(/* webpackChunkName: "apfc" */ "../views/User/AddressList.vue");
const User_AddressUpdate = () =>
  import(/* webpackChunkName: "apfc" */ "../views/User/AddressUpdate.vue");
const routes = [
  {
    path: "/",
    component: Main,
    redirect: "/home",
    meta: {
      title: "容器页",
    },
    children: [
      {
        path: "/home",
        component: Home,
        meta: {
          title: "主页",
        },
      },
      {
        path: "/search",
        component: Search,
        meta: {
          title: "搜索页",
        },
      },
      {
        path: "/categories",
        component: Categories,
        meta: {
          title: "分类",
        },
      },
      {
        path: "/cart",
        component: Cart,
        meta: {
          title: "购物车",
        },
      },
      {
        path: "/user",
        component: User,
        meta: {
          title: "设置",
        },
      },
      {
        path: "/user/address/list",
        component: User_AddressList,
        meta: {
          title: "用户设置-地址列表",
        },
      },
      {
        path: "/user/address/update",
        component: User_AddressUpdate,
        meta: {
          title: "用户设置-地址编辑及新增",
        },
      },
    ],
  },
  {
    path: "/login",
    component: Login,
    meta: {
      title: "登录页",
    },
  },
  {
    path: "/",
    redirect: "home",
  },
  {
    path: "/detail",
    component: Detail,
    meta: {
      title: "详情页",
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
});

router.beforeEach((to, from, next) => {
  // document.title = to.meta.title || '全民消防 “守护者” PMS平台'
  // 无token权限并且要访问的不是登录页出到登录页
  const tokenStr = getCache("token");
  if (!tokenStr && to.path != "/login") {
    return next("/login");
  }
  return next();
});
router.beforeEach((to, from, next) => {
  return next();
});

export default router;
